#include "printSemanticTables.h"

void print_tables(char* name, struct SemanticProgram * program)
{
	QString str;
	FILE* tablesFile = fopen(name, "w");
	fwrite("Constants table:\n",17,1,tablesFile);
	for(int i=0;i<program->mainClass->constantsTable.count();i++)
	{
		str.clear();
		SemanticConstant* row = program->mainClass->constantsTable[i];
		switch(row->type)
		{
		case CONSTANT_UTF8: 
			str.append(QString::number(row->id) + "\t|\tUtf8\t|\t" + row->utf8Const + "\n");
			fwrite(str.toAscii().data(),str.length(),1,tablesFile);
			break;
		case CONSTANT_INTEGER: 
			str.append(QString::number(row->id) + "\t|\tINTEGER\t|\t" + QString::number(row->intConst) + "\n");
			fwrite(str.toAscii().data(),str.length(),1,tablesFile);
			break;
		case CONSTANT_FLOAT:
			str.append(QString::number(row->id) + "\t|\tFLOAT\t|\t" + QString::number(row->floatConst) + "\n");
			fwrite(str.toAscii().data(),str.length(),1,tablesFile);
			break;
		case CONSTANT_CLASS:
			str.append(QString::number(row->id) + "\t|\tClass\t|\t" + QString::number(row->const1->id) + "\n");
			fwrite(str.toAscii().data(),str.length(),1,tablesFile);
			break;
		case CONSTANT_STRING:
			str.append(QString::number(row->id) + "\t|\tSTRING\t|\t" + QString::number(row->const1->id) + "\n");
			fwrite(str.toAscii().data(),str.length(),1,tablesFile);
			break;
		case CONSTANT_FIELDREF: 
			str.append(QString::number(row->id) + "\t|\tFieRef\t|\t" + QString::number(row->const1->id) + "," + QString::number(row->const2->id) + "\n");
			fwrite(str.toAscii().data(),str.length(),1,tablesFile);
			break;
		case CONSTANT_METHODREF: 
			str.append(QString::number(row->id) + "\t|\tMetRef\t|\t" + QString::number(row->const1->id) + "," + QString::number(row->const2->id) + "\n");
			fwrite(str.toAscii().data(),str.length(),1,tablesFile);
			break;
		case CONSTANT_NAMEANDTYPE: 
			str.append(QString::number(row->id) + "\t|\tNandT\t|\t" + QString::number(row->const1->id) + "," + QString::number(row->const2->id) + "\n");
			fwrite(str.toAscii().data(),str.length(),1,tablesFile);
			break;
		}
	}
	fwrite("\nClasses table:\n",16,1,tablesFile);
	str.clear();
	str.append(QString::number(program->mainClass->constClass->id) + "\t|\t" + QString::number(program->mainClass->constClass->const1->id) + "\n");
	fwrite(str.toAscii().data(),str.length(),1,tablesFile);
	fwrite("\nFields table:\n",15,1,tablesFile);
	for(int i=0;i<program->mainClass->fieldsTable.count();i++)
	{
		str.clear();
		SemanticField* row = (program->mainClass->fieldsTable.begin() + i).value();
		str.append(QString::number(row->constFieldref->id) + "\t|\t" + fromTypeToString(row->type) + "\n");
		fwrite(str.toAscii().data(),str.length(),1,tablesFile);
	}
	fwrite("\nMethods table:\n",16,1,tablesFile);
	for(int i=0;i<program->mainClass->methodsTable.count();i++)
	{
		str.clear();
		SemanticMethod* row = (program->mainClass->methodsTable.begin() + i).value();
		str.append(QString::number(row->constMethodref->id) + "\t|\t" + row->constMethodref->const2->const2->utf8Const + "\n");
		fwrite(str.toAscii().data(),str.length(),1,tablesFile);
	}
	fwrite("\nLocal Variables table:\n",24,1,tablesFile);
	for(int i=0;i<program->mainClass->methodsTable.count();i++)
	{
		str.clear();
		SemanticMethod* row = (program->mainClass->methodsTable.begin() + i).value();
		str.append(QString::number(row->constMethodref->id));
		for(int j=0;j<row->localVariableTable.count();j++)
		{
			SemanticLocalVariable* col = (row->localVariableTable.begin() + j).value();
			str.append("\t|\t" + QString::number(col->id) + "\t" + col->name + "\t" + fromTypeToString(col->type));
		}
		str.append("\n");
		fwrite(str.toAscii().data(),str.length(),1,tablesFile);
	}
	fclose(tablesFile);
}